package com.du.stutestsys.serverImpl.AdministratorImpl;

import com.du.stutestsys.dao.ClassMagMapper;
import com.du.stutestsys.dao.ClassTeacherMapper;
import com.du.stutestsys.entity.ClassMag;
import com.du.stutestsys.entity.ClassTeacher;
import com.du.stutestsys.entity.SysUser;
import com.du.stutestsys.service.Administrator.ClassMagService;
import com.du.stutestsys.util.DuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class ClassMagServiceImpl implements ClassMagService {
    @Autowired
    ClassMagMapper classMagMapper;

    @Override
    @Transactional
    public boolean addClassMagAndTeacher(ClassMag classMag,List<ClassMag> classTeachers) {
//        System.out.println(classMag.toString());
//        classTeachers.forEach(System.out::println);
//        新增班级
        boolean add1 =  classMagMapper.insert(classMag)>0;
        for (ClassMag teacher : classTeachers) {
            teacher.setId(classMag.getId());   //保存自增回滚id
            if (teacher.getType().equals("3")){       //保存班主任id用于更新用户表班主任对应班级
                classMag.setTeacher_id(teacher.getTeacher_id());
            }
        }
//        老师表更新老师对应班级
        boolean add2 = classMagMapper.addClassTeacher(classTeachers)>0;
//        用户表更新班主任对应班级id 名字
        boolean add3 = classMagMapper.updateUserTeacher(classMag)>0;
        return DuUtil.isAdds(new Boolean[]{add1,add2,add3});
    }


    @Override
    public boolean updateClassGrade() {
        return classMagMapper.updateClassGrade()>0;
    }

    @Override
    @Transactional
    public boolean updateClassTeacher(ClassMag classMag, Map<SysUser, SysUser> teachers) {
        boolean add1 = updateClassMag(classMag);
        boolean add2 = updateTeacher(teachers);
        return DuUtil.isAdds(new Boolean[]{add1,add2});
    }

    @Override
    public boolean updateClassMag(ClassMag classMag) {
        return classMagMapper.updateClassMag(classMag)>0;
    }

    @Override
    public boolean updateTeacher(Map<SysUser, SysUser> teachers) {
        return classMagMapper.updateClassTeacher(teachers)>0;
    }

    @Override
    public List<ClassMag> selectClassMag(ClassMag classMag) {
        return classMagMapper.selectClassMag(classMag) ;
    }

    @Override
    public boolean relieveClass(List<Long> ids) {
        return classMagMapper.relieveClass(ids)>0;
    }

}
